<?php

namespace App\Admin\Controllers;

use App\Facades\AdminC;
use App\Http\Controllers\Controller;
use App\Models\Member;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class MemberController extends Controller
{
    use ModelForm;

    public function index()
    {
        return AdminC::content(function(Content $content){
            $content->header('用户列表');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    protected function grid()
    {
        return AdminC::grid(Member::class, function(Grid $grid){
            $grid->filter(function($filter){
                $filter->like('name','用户');
                $filter->equal('pid','邀请人id');
            });
            $grid->id('Id')->style('width:10%;');
            $grid->name('用户')->style('width:15%;');
            $grid->pid('邀请人')->style('width:15%;')->display(function($pid){
                if($pid > 0)
                    return Member::find($pid)->name;
                return '';
            });
            $grid->account('零钱余额')->style('width:10%;')->display(function($account){
                return $account.'元';
            });
            $grid->bi('金币余额')->style('width:10%;')->display(function($bi){
                return $bi.'金币';
            });
            $grid->yt_account('已提现')->style('width:10%;')->display(function($yt_account){
                return $yt_account.'元';
            });
            $grid->child_nums('收徒数')->style('width:10%;');
            $grid->created_at('注册时间')->style('width:15%;');

            $grid->disableExport();
            $grid->disableCreateButton();
        });
    }

    public function edit($id)
    {
        return AdminC::content(function(Content $content) use($id){
            $content->header('用户列表');
            $content->description('编辑');
            $content->body($this->form()->edit($id));
        });
    }

    protected function form()
    {
        return AdminC::form(Member::class, function(Form $form){
            $form->display('id', 'Id');
            $form->display('name','用户名');
            $form->display('avatar', '图片');
            $form->select('sex', '性别')->options($form->model()->sex_arr)->readOnly();
            $form->display('account','余额(元)');
            $form->display('yt_account','已提金额');
            $form->display('bi','余币');
            $form->display('yt_bi','已提币');
            $form->display('pid', '邀请人Id');
            $form->display('child_nums', '徒弟数');
            $form->display('created_at','注册时间');
            $form->display('updated_at','更新时间');
        });
    }

}
